; squeak3.8_win_chn_local.nsi
;
; This scripts will download the squeak 3.8 chinese image zip file 
; and Windows Virtual machine from
; ftp.squeak.org and liuxinyu95.googlepages.com
; The purpose of this installer is to provide a "one click" thing
; for the Newbies.

;--------------------------------

;InitPluginsDir
; The name of the installer
Name "Squeak 3.8 Chinese local Installer"

; The file to write
OutFile "squeak3.8chn_win_local.exe"

; The default installation directory
InstallDir "$PROGRAMFILES\Squeak3.8CHN"

;--------------------------------

; Pages

Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles
;--------------------------------

; Languages supported
LoadLanguageFile "${NSISDIR}\Contrib\Language files\English.nlf"
LoadLanguageFile "${NSISDIR}\Contrib\Language files\SimpChinese.nlf"

; Set name using the normal interface (Name command)
LangString Name ${LANG_ENGLISH} "English"
LangString Name ${LANG_SIMPCHINESE} "Simplified Chinese"
;Name $(Name)


;--------------------------------

Var remote_zip_file
Var local_zip_file

; The stuff to install
Section "" ;No components page, name is not important

  ; Set output path to the installation directory.
  SetOutPath $INSTDIR
  
  ; Download files from internet
  Call DownloadVM
  Call DownloadImage
  Call DownloadChange
  Call DownloadSource
  Call DownloadChinese
  
  ; Put file there
  ; File nothing

  ; Create short cut
  CreateDirectory "$SMPROGRAMS\Squeak\3.8CHN\"
  CreateShortcut "$SMPROGRAMS\Squeak\3.8CHN\squeak.lnk" $INSTDIR\Squeak.exe '"$INSTDIR\SqueakPlugin-dev-zh-cn-init.image"'  
  CreateShortcut "$DESKTOP\squeak3.8CHN.lnk" $INSTDIR\Squeak.exe '"$INSTDIR\SqueakPlugin-dev-zh-cn-init.image"'  
  CreateShortcut "$SMPROGRAMS\Squeak\3.8CHN\uninstall.lnk" "$INSTDIR\uninstall.exe"  
  
  WriteUninstaller "$INSTDIR\uninstall.exe"
  
  Exec '$INSTDIR\Squeak.exe "$INSTDIR\SqueakPlugin-dev-zh-cn-init.image"'
  SetAutoClose true
SectionEnd ; end the section

Section "Uninstall"
  ; remove files
  Delete "$INSTDIR\*.image"
  Delete "$INSTDIR\*.changes"
  Delete "$INSTDIR\*.dll"
  Delete "$INSTDIR\*.sources"
  Delete "$INSTDIR\*.exe"
  Delete "$INSTDIR\zh-cn.translation"
  Delete "$INSTDIR\*.zip"
  RMDir  "$INSTDIR"

  ; remove short cuts  
  Delete "$SMPROGRAMS\Squeak\3.8CHN\squeak.lnk"
  Delete "$SMPROGRAMS\Squeak\3.8CHN\uninstall.lnk"
  Delete "$DESKTOP\squeak3.8CHN.lnk"
  RMDir "$SMPROGRAMS\Squeak\3.8CHN"
  SetAutoClose true
SectionEnd 

Function DownloadVM
  StrCpy $remote_zip_file "http://ftp.squeak.org/current_stable/win/Squeak-Win32-3.7.1-VM.zip"
  StrCpy $local_zip_file "Squeak-Win32-3.7.1-VM.zip"
  ;Call CopyFile
  Call ExtractFile
FunctionEnd

Function DownloadImage
  StrCpy $remote_zip_file "http://liuxinyu95.googlepages.com/SqueakPlugin-dev-zh-cn-init.image.zip"
  StrCpy $local_zip_file "SqueakPlugin-dev-zh-cn-init.image.zip"
  ;Call CopyFile
  Call ExtractFile
FunctionEnd

Function DownloadChange
  StrCpy $remote_zip_file "http://liuxinyu95.googlepages.com/SqueakPlugin-dev-zh-cn-init.changes.zip"
  StrCpy $local_zip_file "SqueakPlugin-dev-zh-cn-init.changes.zip"
  ;Call CopyFile
  Call ExtractFile
FunctionEnd

Function DownloadSource
  StrCpy $remote_zip_file "http://liuxinyu95.googlepages.com/SqueakV3.zip"
  StrCpy $local_zip_file "SqueakV3.zip"
  ;Call CopyFile
  Call ExtractFile
FunctionEnd

Function DownloadChinese
  StrCpy $remote_zip_file "http://liuxinyu95.googlepages.com/zh-cn.translation"
  StrCpy $local_zip_file "zh-cn.translation"
  Call CopyFile
FunctionEnd

Function CopyFile
  DetailPrint "Copy [$local_zip_file]"
  CopyFiles "$EXEDIR\$local_zip_file" "$INSTDIR\$local_zip_file"
FunctionEnd

Function ExtractFile
  nsisunz::UnzipToLog "$EXEDIR\$local_zip_file" "$INSTDIR"
    Pop $R0
    StrCmp $R0 "success" unzip_ok
      DetailPrint "$R0" ;print error message to log
      Abort
  unzip_ok:
    DetailPrint "extract $R0 OK"
    Delete "$INSTDIR\local_zip_file"
FunctionEnd

Function .onInit

    ;Language selection dialog
    Push ""
    Push ${LANG_ENGLISH}
    Push English
    Push ${LANG_SIMPCHINESE}
    Push "Simplified Chinese"
    Push A ; A means auto count languages
           ; for the auto count to work the first empty push (Push "") must remain
    LangDLL::LangDialog "Installer Language" "Please select the language of the installer"

    Pop $LANGUAGE
    StrCmp $LANGUAGE "cancel" 0 +2
        Abort
FunctionEnd